package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.ticket.basis.mapper.TicketScrapInfoMapper;
import cn.darkhorse.ticket.basis.pojo.TicketProcedure;
import cn.darkhorse.ticket.basis.pojo.TicketScrapInfo;
import cn.darkhorse.ticket.basis.pojo.TicketUnitInfo;
import cn.darkhorse.ticket.basis.service.TicketProcedureService;
import cn.darkhorse.ticket.basis.service.TicketScrapInfoService;
import cn.darkhorse.ticket.basis.service.TicketUnitInfoService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.basis.vo.TicketScrapInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 废料信息 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-01-10
 */
@Service
public class TicketScrapInfoServiceImpl extends ServiceImpl<TicketScrapInfoMapper, TicketScrapInfo> implements TicketScrapInfoService {
    @Resource
    private RepeatCheck<TicketScrapInfo> repeatCheck;
    @Resource
    private TicketUnitInfoService ticketUnitInfoService;
    @Resource
    private TicketProcedureService ticketProcedureService;
    /**
     * 列表
     *
     * @param dataName
     * @param dataCode
     * @return
     */
    @Override
    public TableDataInfo listAll(String dataName, String dataCode) {
        List<TicketScrapInfoVO> voList=new LinkedList<>();
        LambdaQueryWrapper<TicketScrapInfo> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dataName)){
            wrapper.like(TicketScrapInfo::getName,dataName);
        }
        if(StringUtils.isNotBlank(dataCode)){
            wrapper.like(TicketScrapInfo::getCode,dataCode);
        }
        wrapper.orderByDesc(TicketScrapInfo::getGmtCreate);
        List<TicketScrapInfo> list = list(wrapper);
        List<TicketUnitInfo> unitList = ticketUnitInfoService.list();
        Map<Long, String> collect = unitList.stream().collect(Collectors.toMap(TicketUnitInfo::getId, TicketUnitInfo::getName));
        list.forEach(item->{
            TicketScrapInfoVO vo=new TicketScrapInfoVO();
            BeanUtils.copyProperties(item,vo);
            if(collect.containsKey(item.getUnit())){
                vo.setUnitName(collect.get(item.getUnit()));
            }
            voList.add(vo);
        });
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(voList);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 查看
     *
     * @param dataId
     * @return
     */
    @Override
    public TicketScrapInfoVO view(Long dataId) {
        TicketScrapInfoVO vo=new TicketScrapInfoVO();
        TicketScrapInfo byId = getById(dataId);
        BeanUtils.copyProperties(byId,vo);
        if(null!=byId.getUnit()){
            TicketUnitInfo unit = ticketUnitInfoService.getById(byId.getUnit());
            vo.setUnitName(unit.getName());
        }
        return vo;
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Override
    public boolean saveData(TicketScrapInfo info) {
        //TODO 编码校验
        repeatCheck.repeat(this,info, TicketConstants.BUSINESS_TYPE_FL);
        LambdaQueryWrapper<TicketScrapInfo>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketScrapInfo::getName,info.getName());
        if(null!=info.getId()){
            //TODO 修改校验
            if(isQuote(info.getId())){
                throw new ServiceException("该废料已被工序引用，不允许修改!");
            }
            wrapper.ne(TicketScrapInfo::getId,info.getId());
        }
        long count = count(wrapper);
        if(count>0){
            throw new ServiceException("该废料名称系统已存在！");
        }
        return saveOrUpdate(info);
    }

    /**
     * 删除
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean delete(Long dataId) {
        //TODO 删除校验
        if(isQuote(dataId)){
            throw new ServiceException("该废料已被工序引用，不允许删除!");
        }
        return removeById(dataId);
    }
    /**
     * 是否被引用
     * @param dataId
     * @return
     */
    @Override
    public boolean isQuote(Long dataId){
        LambdaQueryWrapper<TicketProcedure> wrapper=new LambdaQueryWrapper<>();
        wrapper.apply(dataId!=null ,"FIND_IN_SET ('" + dataId + "',wastes)");
        long count = ticketProcedureService.count(wrapper);
        if(count>0){
            return true;
        }
        return false;
    }
}
